package com.tron.xml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap;

public class XmlStart {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("src/main/java/com/tron/xml/tron.xml");
        // 返回根元素
        Element root = document.getDocumentElement();
        JSONObject test = parseNode(root);
        JSONObject rootJson = new JSONObject();
        rootJson.put(root.getTagName(), test);
        System.out.println(rootJson.toString(SerializerFeature.PrettyFormat));
    }

    public static JSONObject parseNode(Node node) {
        // 创建对象
        JSONObject elementJson = new JSONObject();
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if (item instanceof Element) {
                if (item.getFirstChild() == item.getLastChild()) {
                    NamedNodeMap attributes = item.getAttributes();
                    int length = attributes.getLength();
                    if (length > 0) {
                        JSONObject jsonObject = new JSONObject();
                        for (int j = 0; j < length; j++) {
                            Node attr = attributes.item(j);
                            jsonObject.put("_" + attr.getNodeName(), attr.getTextContent());
                        }
                        jsonObject.put("#text", item.getTextContent());
                        elementJson.put(item.getNodeName(), jsonObject);
                    }else {
                        elementJson.put(item.getNodeName(), item.getTextContent());
                    }
                } else {
                    String nodeName = item.getNodeName();
                    NamedNodeMap attributes = item.getAttributes();
                    HashMap<String, String> attrMap = null;
                    int length = attributes.getLength();
                    if (length > 0) {
                        attrMap = new HashMap<>();
                        for (int j = 0; j < length; j++) {
                            Node attr = attributes.item(j);
                            attrMap.put("_" + attr.getNodeName(), attr.getTextContent());
                        }
                    }
                    JSONObject jsonObject = parseNode(item);
                    if (null != attrMap) {
                        attrMap.forEach(jsonObject::put);
                    }
                    Object oldValue = elementJson.get(nodeName);
                    if (oldValue != null) {
                        if (oldValue instanceof JSONObject) {
                            JSONArray jsonArray = new JSONArray();
                            jsonArray.add(oldValue);
                            jsonArray.add(jsonObject);
                            elementJson.put(nodeName, jsonArray);
                        } else if (oldValue instanceof JSONArray) {
                            ((JSONArray) oldValue).add(jsonObject);
                            elementJson.put(nodeName, oldValue);
                        }
                    } else {
                        elementJson.put(nodeName, jsonObject);
                    }
                }
            }
        }
        return elementJson;
    }
}
